﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PickingBusiness;
using Infrastructure;
using LabelManager2;
using System.Threading;

namespace Picking_Winform
{
    public partial class StartUpView : FormBase
    {
        private readonly string Title = "配送系统";
        public StartUpView()
        {
            InitializeComponent();
            this.Controls.Add(this.txtTitle);
            Load += new EventHandler(StartUpView_Load);
        }

        protected override int GetFormWidth()
        {
            return 765;
        }

        /// <summary>
        /// Init AIPlug
        /// </summary>
        private void InitAIPlug()
        {
            if (!string.IsNullOrWhiteSpace(ConfigInfo.AxAiPlugIpAddress))
            {
                axAiPlug.ConnectSocket(ConfigInfo.AxAiPlugIpAddress, ConfigInfo.AxAiPlugPort);

                axAiPlug.StartPolling(); //watch the data arrival

                axAiPlug.BinaryMode = true; //set to true

                axAiPlug.CommandTimeout = 5000; //set to 5000
            }
        }

        /// <summary>
        /// Turn On LED Yellow
        /// </summary>
        private void TurnOnLEDYellow()
        {
            //this.axAiPlug.SendCommand("T" + ConfigInfo.CmdLEDYellow.Trim());
        }

        void StartUpView_Load(object sender, EventArgs e)
        {
            ConfigInfo.InitConfigInfo();
            
            string lineName = ConfigInfo.LineName;
            txtTitle.Text = lineName + Title;
            this.Text = lineName + Title;

            this.InitAIPlug();
        }

        private void btnShutdownSystem_Click(object sender, EventArgs e)
        {
            //this.axAiPlug.SendCommand("Z");
            this.CloseCurrentWindow();
        }

        private void btnCheckPoint_Click(object sender, EventArgs e)
        {
            CheckSpecifiedPointView form = new CheckSpecifiedPointView();
            form.FormType = CheckPointFormType.CheckAllPoints;
            form.ShowDialog(this);
        }

        private void btnDistributionOperation_Click(object sender, EventArgs e)
        {
            FormBase form = new DistributionOperationView();
            form.ShowDialog(this);
        }

        private void btnSetupEnvironment_Click(object sender, EventArgs e)
        {
            this.TurnOnLEDYellow();
            FormBase form = new SetupEnvironmentView();
            form.ShowDialog(this);
        }

        private void btnCheckSpecifiedPoint_Click(object sender, EventArgs e)
        {
            CheckSpecifiedPointView form = new CheckSpecifiedPointView();
            form.FormType = CheckPointFormType.CheckSpecifiedPoints;
            form.ShowDialog(this);
        }

        private void btnManualOperation_Click(object sender, EventArgs e)
        {
            FormBase form = new ManualOperationView();
            form.ShowDialog(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(() =>
                {
                    string filePath = AppDomain.CurrentDomain.BaseDirectory + "../../../LibraryBin/Template.lab";
                    string printerName = "";//set printer name
                    LabelManager2.ApplicationClass application = new LabelManager2.ApplicationClass();
                    LabelManager2.Document doc = application.Documents.Open(filePath, true);
                    if (printerName != null) doc.Printer.SwitchTo(printerName);
                    FormVariables vars = doc.Variables.FormVariables;

                    //barcode：01012341001D
                    LabelManager2.Free free1 = doc.Variables.FormVariables.Item("B");
                    free1.Value = "R1";//line name
                    LabelManager2.Free free2 = doc.Variables.FormVariables.Item("WC");
                    free2.Value = "310";//line code
                    LabelManager2.Free free3 = doc.Variables.FormVariables.Item("SEBANGO1");
                    free3.Value = "1234";//背番号
                    LabelManager2.Free free4 = doc.Variables.FormVariables.Item("ALL");
                    free4.Value = "10";//残留台数
                    LabelManager2.Free free5 = doc.Variables.FormVariables.Item("JUNO");
                    free5.Value = "01";//顺番
                    LabelManager2.Free free6 = doc.Variables.FormVariables.Item("JIBU");
                    free6.Value = "010";//电装品WC
                    doc.PrintLabel(1);
                    doc.FormFeed();
                    doc.Close(false);
                    application.Quit();
                }));
            thread.IsBackground = true;
            thread.Start();
        }

        private void btnConfigSetup_Click(object sender, EventArgs e)
        {
            FormBase form = new ConfigSetupView();
            form.ShowDialog(this);
        }
    }
}
